package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.CheckItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @ClassName CheckItemServiceImpl
 * @Description: TODO
 * @Author 郑乾
 * @Date 2020-06-20
 * @Version V1.0
 **/
@Service
@Slf4j
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    CheckItemDao checkItemDao;

    @Override
    public void add(CheckItem checkItem) {
        log.debug("service......" + checkItem);
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        //开始分页
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //条件查询
        Page<CheckItem> page = checkItemDao.findByCoundition(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(), page);
    }

    @Override
    public void delById(Integer id) {
        //查询该检查项是否和检查组有关联 如果有 则提示无法删除
        long count = checkItemDao.findCountByCheckItemId(id);
        if (count > 0) {
            log.debug("当前检查项被引用");
            throw new BusinessRuntimeException("该检查项被引用 无法删除");
        }
        //如果没有被引用 则删除
        checkItemDao.delById(id);
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
}
